package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.List;

import org.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.DoctorPatient;
import com.yksj.healthtalk.ui.R;

/**
 * 
 * 用户列表
 * @author zhao
 *
 */
public class UserListAdapter extends BaseAdapter {
	List<DoctorPatient> list = new ArrayList<DoctorPatient>();
	Activity mActivity;
	ItemOnClickListener mClickListener;
	int mType;
	private final LayoutInflater inflater;
	private ImageLoader mImageloader;
	ItemHeaderOnClickListener mHeaderOnClickListener;
	public interface ItemOnClickListener{
		void onRightClick(int positon,View view,Object object);
	}
	
	public interface ItemHeaderOnClickListener{
		void onHeaderClick(int positon,View view,Object object);
	} 
	
	public UserListAdapter(Activity activity,OnClickListener listener,OnClickListener Headerlistener,int type) {
		this.mActivity = activity;
		this.mClickListener = (ItemOnClickListener) listener;
		this.mHeaderOnClickListener=(ItemHeaderOnClickListener)Headerlistener;
		this.mType = type;
		inflater = LayoutInflater.from(activity);
		mImageloader = ImageLoader.getInstance();
		
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView==null){
			holder=new ViewHolder();
			convertView = onCreateView(convertView,holder);	
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		onBindData(position, holder,convertView);
		return convertView;
	}
	
	static class ViewHolder{
		TextView nickName;//昵称
		TextView startTime;//开始时间
		ImageView head_icon;//头像
		TextView serviceType;//服务类型
		Button actionButton;
	}
	
	private View onCreateView(View view,ViewHolder holder){
		View view2 = null;
		if(view != null)return view;
		switch (mType) {
		case 180://我的患者--(已开始服务,已取消预约,已预约服务)
			view2 = inflater.inflate(R.layout.doctor_patient_item, null);
			holder.nickName=(TextView) view2.findViewById(R.id.nickname);
			holder.head_icon=(ImageView) view2.findViewById(R.id.head_image);
			holder.serviceType=(TextView) view2.findViewById(R.id.service_type);
			holder.actionButton=(Button) view2.findViewById(R.id.onclick);
			holder.startTime=(TextView)view2.findViewById(R.id.start_time);
			break;
		}
		view2.setTag(holder);
		return view2;
	}
	
	private void onBindData(final int position,final ViewHolder holder,final View view){
		final DoctorPatient patient = list.get(position);
		switch (mType) {
		case 180:
			holder.nickName.setText(patient.getCustomerNickname());
			mImageloader.displayImage(patient.getCustomerSex(),
					patient.getClientIconBackground(),
					holder.head_icon);
			if("".equals(patient.getServiceStart())){
				holder.startTime.setVisibility(View.GONE);	
			}else{
//				holder.startTime.setText(patient.getServiceStart().substring(0,patient.getServiceStart().length()-3));	
				holder.startTime.setText(patient.getServiceStart());
			}
			holder.serviceType.setText(patient.getServiceTypeName());
			
			holder.actionButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(mClickListener != null)mClickListener.onRightClick(position,view, patient);
				}
			});
			holder.head_icon.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(mClickListener != null)mHeaderOnClickListener.onHeaderClick(position,view, patient);
				}
			});
			
			switch (Integer.valueOf(patient.getServiceStatusCode())) {
			case 10:
			case 160:
			case 170:
				holder.actionButton.setText("查看详情");
//				holder.actionButton.setText("停止服务");
				break;
			case 150:
				holder.actionButton.setText("延时请求");
				break;
			case 60:
				holder.actionButton.setText("退订请求");
				break;
			case 185:
				holder.actionButton.setText("停止服务");
				break;
			case 175:
			case 180:
				holder.actionButton.setText("取消原因");
				break;
			case 70:
			case 155:
//				holder.actionButton.setText("延时服务");
				holder.actionButton.setText("查看详情");
				break;
			default:
				if(Integer.valueOf(patient.getServiceStatusCode())==50){
				/*	if(TimeUtil.getTimeByString(patient.getServiceStart())){
//						holder.actionButton.setText("取消预约");//开始服务时间<=当前系统时间
						holder.actionButton.setText("查看详情");
					}else{
//						holder.actionButton.setText("延时服务");//已预约服务（50）+开始服务时间>当前系统时间
						holder.actionButton.setText("查看详情");
					}*/
					holder.actionButton.setText("查看详情");
				}else{
					holder.actionButton.setVisibility(View.INVISIBLE);	
				}
				break;
			}
		}
	}
	
	public void updateData(List<DoctorPatient> list){
		this.list.clear();
		this.list.addAll(list);
		notifyDataSetChanged();
	}
}
